<template>
	<div>
		<HyArrayForm
			:hyArrayFormConfig="hyArrayFormConfig"
			:title="'清单'"
			ref="hyArrayFormRef"
			:hyArrayFormFields="hyArrayFormFields"
			:hyArrayFormDefault="reformListDefault"
		/>
	</div>
</template>
<script setup lang="ts">
import HyArrayForm from '/@/components/HyArrayForm/index.vue';
import { defaultValue } from '../../utils/default';
import { ElMessage } from 'element-plus';

const formType: any = inject('formType', 'submit');
const { reformListDefault } = defaultValue();

//动态表单配置
const hyArrayFormFields = computed(() => {
	return {
		reformListId: {
			type: 'input',
			hidden: true,
		},
		reformMatters: {
			label: '改革清单',
			type: 'textarea',
			required: true,
			placeholder: '请一句话描述改革事项，2000字以内。示例：一般以“建立xxx模式”“建立xxx机制”“创新xxx模式”“创新xxx机制”“改进xxx机制”的形式填写',
		},
		specificMeasures: {
			label: '措施及成效',
			type: 'textarea',
			required: true,
			placeholder:
				'请概要综述推进该改革事项的具体举措及成效。示例：一般以“创建......机制，搭建......平台，改变......做法，将......改为......”的结构填写',
		},
	};
});
const hyArrayFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		span: 22,
		offset: 0,
		type: formType.value,
	};
});
const props: any = defineProps({
	reformListData: {
		type: Array,
		default: () => {
			return [];
		},
	},
});
const hyArrayFormRef = ref();

watch(
	() => props,
	(nv1: any) => {
		const { reformListData } = nv1;
		//修改/编辑/新增，如果有数据，展示数据，如果没有，展示输入框
		if (formType.value === 'submit') {
			nextTick(() => {
				if (reformListData && reformListData.length > 0) {
					hyArrayFormRef.value.cancel();
					reformListData.forEach((item: any) => {
						hyArrayFormRef.value.add(item);
					});
				} else {
					hyArrayFormRef.value.add();
				}
			});
		} else {
			nextTick(() => {
				if (reformListData && reformListData.length > 0) {
					reformListData.forEach((item: any) => {
						hyArrayFormRef.value.add(item);
					});
				}
			});
		}
	},
	{ deep: true, immediate: true }
);
const check = async () => {
	const result = await hyArrayFormRef.value.check();
	if (!result) {
		ElMessage.error('表单中有必填项未填写，请检查并补全');
	}
	return result;
};
const getData = () => {
	const reformListList = hyArrayFormRef.value.submit();
	return reformListList;
};
defineExpose({ getData, check });
</script>
<style scoped lang="scss"></style>
